package com.yipei.business.service.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlTradeEntrustOrder;
import com.yipei.business.mapper.MlTradeEntrustOrderMapper;
import com.yipei.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yipei.business.mapper.MlAssetFlowMapper;
import com.yipei.business.domain.MlAssetFlow;
import com.yipei.business.service.IMlAssetFlowService;

/**
 * 资金流水Service业务层处理
 *
 * @author yipei
 * @date 2024-12-23
 */
@Service
public class MlAssetFlowServiceImpl extends ServiceImpl<MlAssetFlowMapper, MlAssetFlow> implements IMlAssetFlowService
{
    @Autowired
    private MlAssetFlowMapper mlAssetFlowMapper;

    /**
     * 查询资金流水
     *
     * @param id 资金流水主键
     * @return 资金流水
     */
    @Override
    public MlAssetFlow selectMlAssetFlowById(Long id)
    {
        return mlAssetFlowMapper.selectMlAssetFlowById(id);
    }

    /**
     * 查询资金流水列表
     *
     * @param mlAssetFlow 资金流水
     * @return 资金流水
     */
    @Override
    public List<MlAssetFlow> selectMlAssetFlowList(MlAssetFlow mlAssetFlow)
    {
        return mlAssetFlowMapper.selectMlAssetFlowList(mlAssetFlow);
    }

    /**
     * 新增资金流水
     *
     * @param mlAssetFlow 资金流水
     * @return 结果
     */
    @Override
    public int insertMlAssetFlow(MlAssetFlow mlAssetFlow)
    {
        mlAssetFlow.setCreateTime(DateUtils.getNowDate());
        return mlAssetFlowMapper.insertMlAssetFlow(mlAssetFlow);
    }

    /**
     * 修改资金流水
     *
     * @param mlAssetFlow 资金流水
     * @return 结果
     */
    @Override
    public int updateMlAssetFlow(MlAssetFlow mlAssetFlow)
    {
        mlAssetFlow.setUpdateTime(DateUtils.getNowDate());
        return mlAssetFlowMapper.updateMlAssetFlow(mlAssetFlow);
    }

    /**
     * 批量删除资金流水
     *
     * @param ids 需要删除的资金流水主键
     * @return 结果
     */
    @Override
    public int deleteMlAssetFlowByIds(Long[] ids)
    {
        return mlAssetFlowMapper.deleteMlAssetFlowByIds(ids);
    }

    /**
     * 删除资金流水信息
     *
     * @param id 资金流水主键
     * @return 结果
     */
    @Override
    public int deleteMlAssetFlowById(Long id)
    {
        return mlAssetFlowMapper.deleteMlAssetFlowById(id);
    }

    @Override
    public void creatObject(String tdAcc, int i, BigDecimal lsje) {
        MlAssetFlow mlAssetFlow = new MlAssetFlow();
        mlAssetFlow.setTdAcc(tdAcc);
        mlAssetFlow.setLsLx((long) i);
        mlAssetFlow.setLsJe(lsje);
        mlAssetFlow.setLsDh(this.createLSDH());
        mlAssetFlow.setCreateTime(new Date());
        this.save(mlAssetFlow);
    }

    private String createLSDH() {
        Date date = new Date();
        DateFormat formatter= new SimpleDateFormat("yyyyMMdd");
        String format = formatter.format(date);
        Random random = new Random();
        int randomNumber = random.nextInt(100000);
        String formattedNumber = String.format("%05d", randomNumber);
        return "ZL" +format+formattedNumber;
    }
}
